package com.pzh.xzh.uaa.handler;


import com.pzh.xzh.uaa.result.Result;
import com.pzh.xzh.uaa.result.ResultCodeEnum;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;

/**
 * 全局异常 统一处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
      //  e.printStackTrace();
       return Result.error();
    }
    @ExceptionHandler(BadSqlGrammarException.class)
    @ResponseBody
    public Result error(BadSqlGrammarException e){
        return Result.setResult(ResultCodeEnum.SQL_BAD);
    }
    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseBody
    public Result error(HttpMessageNotReadableException e){
        return Result.setResult(ResultCodeEnum.PARAM_ERROR);
    }
    //自定义异常
    @ExceptionHandler(HandlerException.class)
    @ResponseBody
    public Result error(HandlerException e){
        return Result.error().code(e.getCode()).message(e.getMessage());
    }

}
